home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
singleinstance.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
6KB
|
183 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from __future__ import with_statement
import sys
import socket
import wx
import os
import logging
import threading
from time import time
from traceback import print_exc
class SingleInstanceApp(wx.App):
def __init__(self, appname, *args, **kws):
appname = '%s-%s' % (appname, wx.GetUserId())
mgr = InstanceChecker(appname, 'localhost', InstanceChecker.default_port)
self.instance_checker = mgr
try:
if mgr.isAnotherRunning() and mgr.sendRaisePreviousFrameCommand():
print 'instance already running. quitting!'
sys.exit(0)
except Exception:
print_exc()
wx.App.__init__(self, *args, **kws)
def StopSingleInstanceServer(self):
return self.instance_checker.stopServer()
def SetTopWindow(self, w):
wx.App.SetTopWindow(self, w)
instcheck = self.instance_checker
instcheck.setFrame(w)
if not instcheck.isServerRunning():
instcheck.startServer()
def MainLoop(self, *args, **kws):
if not hasattr(self, 'instance_checker'):
raise AssertionError('must call SetTopWindow on this app first')
try:
wx.App.MainLoop(self, *args, **kws)
finally:
self.instance_checker.stopServer()
class ServerThread(threading.Thread):
backlog = 5
def __init__(self, host, port, function, timeout = None):
threading.Thread.__init__(self, name = self.__class__.__name__ + '-' + host + ':' + str(port))
self.host = host
self.port = port
self.function = function
self.die = False
if not timeout:
pass
self.timeout = 0.2
def run(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((self.host, self.port))
s.listen(self.backlog)
s.settimeout(self.timeout)
while not self.die:
try:
(client, address) = s.accept()
self.function()
client.close()
continue
except socket.timeout:
e = None
continue
None<EXCEPTION MATCH>socket.timeout
s.close()
def isRunning(self):
return not (self.die)
def stop(self):
self.die = True
def poke_client_port(host, port):
try:
s = socket._orgsocket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.close()
except Exception:
return False
return True
class InstanceChecker(object):
default_port = 8791
def __init__(self, name, host, port, frame = None, func = None):
self.name = name
self.frame = frame
self.port = port
self.host = host
self.logger = logging.getLogger('')
if not func:
self.func = lambda : wx.CallAfter(self._InstanceChecker__raiseFrame)
self.s_checker = wx.SingleInstanceChecker(self.name)
def startServer(self):
self.logger.info('Server stuff')
self.server = ServerThread(self.host, self.port, self.func)
self.server.start()
def sendRaisePreviousFrameCommand(self):
self.logger.info('Poking IPC loopback connection')
return poke_client_port(self.host, self.port)
def isServerRunning(self):
if hasattr(self, 'server'):
pass
return self.server.isRunning()
def stopServer(self):
if hasattr(self, 's_checker'):
del self.s_checker
if self.server.isRunning():
self.server.stop()
return True
else:
self.logger.warning("Tried to stop a server that wasn't running")
return False
def setFrame(self, f):
self.frame = f
def setFunc(self, func):
self.func = func
def __raiseFrame(self):
self.frame.Show(True)
self.frame.Iconize(False)
self.frame.Raise()
def isAnotherRunning(self):
return self.s_checker.IsAnotherRunning()
if __name__ == '__main__':
app = SingleInstanceApp(0)
f = wx.Frame(None, -1, 'This app only runs once!')
f.Show(True)
app.SetTopWindow(f)
app.MainLoop()